package com.rahms.server.controller;

import com.rahms.common.constant.MessageConstant;
import com.rahms.common.result.R;
import com.rahms.pojo.entity.HealthData;
import com.rahms.server.service.HealthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 健康数据控制器
 */
@RestController
@RequestMapping("/health-data")
public class HealthDataController {
    
    @Autowired
    private HealthDataService healthDataService;
    
    /**
     * 根据老人ID和数据类型获取健康数据
     */
    @GetMapping("/by-type")
    public R<List<HealthData>> getHealthDataByElderlyIdAndType(@RequestParam Long elderlyId,
                                                             @RequestParam String dataType) {
        List<HealthData> list = healthDataService.getHealthDataByElderlyIdAndType(elderlyId, dataType);
        return R.success(list);
    }
    
    /**
     * 根据日期范围获取健康数据
     */
    @GetMapping("/by-date")
    public R<List<HealthData>> getHealthDataByDateRange(@RequestParam Long elderlyId,
                                                      @RequestParam(required = false) String dataType,
                                                      @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                                      @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        List<HealthData> list = healthDataService.getHealthDataByDateRange(elderlyId, dataType, startDate, endDate);
        return R.success(list);
    }
    
    /**
     * 获取健康数据统计信息
     */
    @GetMapping("/statistics")
    public R<Map<String, Object>> getHealthDataStatistics(@RequestParam Long elderlyId,
                                                        @RequestParam String dataType,
                                                        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                                        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
        Map<String, Object> statistics = healthDataService.getHealthDataStatistics(elderlyId, dataType, startDate, endDate);
        return R.success(statistics);
    }
    
    /**
     * 添加健康数据
     */
    @PostMapping("/add")
    public R<Boolean> addHealthData(@RequestBody HealthData healthData) {
        boolean result = healthDataService.save(healthData);
        return result ? R.success(true).add("message", MessageConstant.HEALTH_DATA_ADD_SUCCESS) : R.error(MessageConstant.SAVE_FAILED);
    }
    
    /**
     * 更新健康数据
     */
    @PostMapping("/update")
    public R<Boolean> updateHealthData(@RequestBody HealthData healthData) {
        boolean result = healthDataService.updateById(healthData);
        return result ? R.success(true).add("message", MessageConstant.HEALTH_DATA_UPDATE_SUCCESS) : R.error(MessageConstant.UPDATE_FAILED);
    }
}